<template>
  <el-cascader v-model="damageType" :props="props" :show-all-levels="false" @change="onChanged" />
</template>

<script>
import { getAllDamageType } from '@/api/dailyInspection'
export default {
  name: 'DamageTypeCascader',
  data() {
    return {
      damageType: [],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node
          let params = {}
          if (level === 1) {
            params = {
              pavementTypeId: node.value
            }
          } else if (level === 2) {
            params = {
              damageCategoryId: node.value
            }
          }
          getAllDamageType(params).then(datas => {
            const nodes = []
            datas.forEach(data => {
              nodes.push({
                value: data.damageTypeId || data.damageCategoryID || data.pavementTypeID,
                label: data.pavementTypeName || data.damageCategotyName || data.damageTypeName,
                leaf: level >= 2
              })
            })
            resolve(nodes)
          })
        }
      }
    }
  },
  methods: {
    onChanged(value) {
      this.$emit('valueChanged', value[2])
    }
  }
}
</script>

<style scoped>

</style>
